package com.lcj.controller2;

import com.lcj.vo.UserVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/req")
public class HttpServletRequestController {

    @RequestMapping("/f1")
    public String f1(HttpServletRequest request){
        UserVo user = new UserVo();
        user.setUsername("admin");
        request.setAttribute("user",user);
        return "success";
    }

    @RequestMapping("/f2")
    public String f2(UserVo user){
        user.setUsername("f2");
        return "success";
    }

    @RequestMapping("/f3")
    public String f3(@ModelAttribute UserVo user){
        user.setUsername("f3");
        return "success";
    }

    @RequestMapping("/f4")
    public String f4(@ModelAttribute("u") UserVo user){
        user.setUsername("f4");
        return "success";
    }


    @ModelAttribute("users")
    public List<UserVo> f5(){
        List<UserVo> users = new ArrayList<>();
        UserVo user = new UserVo();
        user.setAge(1);
        user.setUsername("admin");

        UserVo user2 = new UserVo();
        user2.setAge(2);
        user2.setUsername("alice");

        UserVo user3 = new UserVo();
        user3.setAge(3);
        user3.setUsername("jack");

        users.add(user);
        users.add(user2);
        users.add(user3);
        return users;
    }
}
